/**
 * Created by wpj on 17-10-21.
 */

/**
 *
 * 登陆
 * @param baseParams
 * @returns {*}
 */

var login = function (baseParams) {

    return new Vue({
        el: '#app',
        data: {
            rules: {
                userName: [
                    { required: true, message: '请输入账号名称', trigger: 'blur' }
                ],
                passWord: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }, // 一些验证规则
            loginData: {
                userName: '',
                passWord: '',
                rememberMe: false
            },
            passInputType: '', // 密码是否显示明文
            submitLoading: false // 登陆按钮 正在加载中

        },
        created: function () {
            var params = $w.getParams('showHint');
            if (parseInt(params) === 1) {
                this.$message({
                    type: 'warning',
                    showClose: true,
                    message: '请先登录您的账号'
                });
            }
        },
        methods: {
            /**
             * 登陆
             */
            login: function () {

                // 按钮正在加载
                this.submitLoading = true;
                var that = this;

                // 先验证下
                this.$refs['loginForm'].validate(function (valid) {
                    // 验证不过
                    if (!valid) {
                        that.submitLoading = false;
                        return false;
                    }

                    // 验证成功 - 继续发送请求
                    // 获取各模块的值
                    $.ajax({
                        url: $w.getApiUrl('login'),
                        type: 'POST',
                        data: {
                            username: that.loginData.userName,
                            password: that.loginData.passWord,
                            rememberMe: (that.loginData.rememberMe === true) ? 1 : 0
                        },
                        dataType: "json",
                        success: function (event) {

                            // 按钮正在加载
                            that.submitLoading = false;

                            // 操作失败显示错误信息
                            if (parseInt(event.no) !== 200) {

                                that.$message({
                                    type: 'error',
                                    showClose: true,
                                    message: event.msg
                                });
                                return false;
                            }

                            window.location.href = $w.getPageUrl('index');
                        },
                        error: function (event) {

                            // 按钮正在加载
                            that.submitLoading = false;
                            that.$message({
                                type: 'error',
                                showClose: true,
                                message: '操作频繁，请稍后尝试'
                            });
                            return false;
                        }
                    });
                });
            },
            /**
             * 隐藏|显示 密码框密码
             */
            showHidePass: function () {
                if (!this.passInputType || this.passInputType === 'text') {
                    this.passInputType = 'password';
                    return true;
                } else {

                    this.passInputType = 'text';
                    return true;
                }
            }
        }
    });
};
